﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Results;
using HCQ2_Common.Constant;
using HCQ2_IBLL;
using HCQ2_Model.ClassRoom;
using HCQ2_Model.ViewModel;
using HCQ2_Model.ViewModel.WebAPI;
using HCQ2WebAPI_Logic.BaseAPIController;
using HCQ2WebAPI_Logic.Filters;
using HCQ2_Model.VLine.InputDto;
using HCQ2WebAPI_Logic.Model;
using HCQ2UI_Helper.Session;
using HCQ2_Model;
using HCQ2_Model.VR.Params;
using HCQ2UI_Helper;
using HCQ2_Model.VRDD.OutputDto._211026;

namespace HCQ2WebAPI_Logic.DocumentController
{
    /// <summary>
    /// 收货地址管理控制器
    /// 创建人：Joychen
    /// </summary>
    public class VRAddressAPIController : BaseWeiXinApiLogic
    {
        #region 1.0 新增收货地址 + AddAddress(AddressParam dto)
        /// <summary>
        /// 1.0 新增收货地址
        /// </summary>
        /// <param name="dto">参数</param>
        /// <returns></returns>
        /// <remarks>
        /// 1、需求变更： 禅道id：224 日期：2021-11-12<br/>
        /// </remarks>
        [HttpPost]
        [ModelValid]
        public WebApiResultJsonModel AddAddress(AddressParam dto)
        {
            T_User user = OperateContext.Current.bllSession.T_User.FindSingle(s => s.user_guid.Equals(dto.userid));
            if(user==null)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "userid代码异常~", "");
            dto.user_id = user.user_id;
            var obj = HCQ2_Common.Helper.AutoMapperConvert<T_VRAddress, AddressParam>(dto);
            obj.create_id = user.user_id;
            obj.update_id= user.user_id;
            obj.create_date = DateTime.Now;
            obj.create_name = user.user_name;
            obj.update_name = user.user_name;
            obj.update_date = DateTime.Now;
            int result = operateContext.bllSession.T_VRAddress.Add(obj);
            if (result > 0)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "添加收货地址成功~", obj.id);
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Error, "添加收货地址失败~", "");
        }
        #endregion

        #region 1.1 编辑收货地址 + ModifyAddress(AddressParam dto)
        /// <summary>
        /// 1.1 编辑收货地址
        /// </summary>
        /// <param name="dto">参数</param>
        /// <returns></returns>
        /// <remarks>
        /// 1、需求变更： 禅道id：225 日期：2021-11-12<br/>
        /// </remarks>
        [HttpPost]
        [ModelValid]
        public WebApiResultJsonModel ModifyAddress(AddressParam dto)
        {
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            if(dto.id<=0)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "编辑信息主键id不允许为空！", null);
            T_User user = OperateContext.Current.bllSession.T_User.FindSingle(s => s.user_guid.Equals(dto.userid));
            if (user == null)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "userid代码异常~", "");
            var obj = HCQ2_Common.Helper.AutoMapperConvert<T_VRAddress, AddressParam>(dto);
            obj.user_id = user.user_id;
            obj.update_id = user.user_id;
            obj.update_name = user.user_name;
            obj.update_date = DateTime.Now;
            int mark = operateContext.bllSession.T_VRAddress.ModifyAddr(obj);
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "编辑收货地址成功~", "");
        }
        #endregion

        #region 1.2 删除收货地址 + GetSysLineUserNum(SysUserInputDto unitDto)
        /// <summary>
        /// 1.2 删除收货地址
        /// </summary>
        /// <param name="obj">参数</param>
        /// <returns></returns>
        [HttpPost]
        [ModelValid]
        public WebApiResultJsonModel DeleteAddressById(BaseAddressParam obj)
        {
            if(obj.id<=0)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "删除关键字不能为空~", "");
            //判断是否是自己创建的地址
            int user_id = SysPermissSession.ChangeIdByGuid(obj.userid);
            var query = operateContext.bllSession.T_VRAddress.FindSingle(s => s.id == obj.id);
            if(query==null)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "待删除的数据不存在~", "");
            if(query.create_id!=user_id)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "待删除的数据不是你所创建的，您无权删除~", "");
            int result = operateContext.bllSession.T_VRAddress.Delete(s => s.id == obj.id);
            if(result>0)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "数据删除成功~", "");
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Error, "数据删除失败~", "");
        }
        #endregion

        #region 1.3 获取收货地址列表 + WebApiResultJsonModel GetSourceFileList(DocListParam model)
        /// <summary>
        ///  1.3 获取收货地址列表
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        /// <remarks>
        /// 1、需求变更： 禅道id：223 日期：2021-11-12<br/>
        /// 2、需求变更： 禅道id：353 日期：2022-06-28<br/>
        /// </remarks>
        [HttpPost]
        [ModelValid]
        public WebApiResultJsonModel<List<OutputAddress>> GetAddressList(BaseAddressParam obj)
        {
            if (!ModelState.IsValid)
                return ApiResultJsonModel<List<OutputAddress>>.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            int user_id = SysPermissSession.ChangeIdByGuid(obj.userid);
            List<T_VRAddress> list = operateContext.bllSession.T_VRAddress.Select(s => s.user_id == user_id);
            if(list==null || list.Count<=0)
                return ApiResultJsonModel<List<OutputAddress>>.RedirectWebApi(WebResultCode.Exception, "尚未设置收获地址！", null);
            List<OutputAddress> addList = new List<OutputAddress>();
            list?.ForEach(s => addList.Add(new OutputAddress
            {
                id = s.id,
                user_id = s.user_id,
                user_name = s.user_name,
                phone = s.phone,
                area = s.area,
                addr = s.addr,
                addr_mark = s.addr_mark,
                is_default = s.is_default,
                create_id = s.create_id,
                create_date = s.create_date,
                create_name = s.create_name,
                update_id = s.update_id,
                update_name = s.update_name,
                update_date = s.update_date,
                gps_x = s.gps_x,
                gps_y = s.gps_y,
                areaID = s.areaID
            }));
            if (obj.isDefault==0)
                return ApiResultJsonModel<List<OutputAddress>>.RedirectWebApi(WebResultCode.Ok, "获取收货地址列表成功~", addList);
            List<OutputAddress> result = new List<OutputAddress>();
            result = addList.Where(s => s.is_default)?.ToList();
            if (result == null || result.Count <= 0)
                result = addList.OrderByDescending(s => s.create_date).Take(1)?.ToList();
            return ApiResultJsonModel<List<OutputAddress>>.RedirectWebApi(WebResultCode.Ok, "获取收货地址列表成功~", result);
        }
        #endregion

        #region 1.4 更改默认收货地址 + ModifyAddressDefault(ModifyAddressParam dto)
        /// <summary>
        /// 1.4 更改默认收货地址
        /// </summary>
        /// <param name="dto">参数</param>
        /// <returns></returns>
        [HttpPost]
        [ModelValid]
        public WebApiResultJsonModel ModifyAddressDefault(ModifyAddressParam dto)
        {
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            int user_id = SysPermissSession.ChangeIdByGuid(dto.userid);
            if(user_id<=0)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "userid用户编码参数有误~", "");
            var obj = operateContext.bllSession.T_VRAddress.FindSingle(s => s.id == dto.id);
            if(obj==null)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "收获地址ID参数有误~", "");
            //设置为默认地址
            if (dto.status == 1)
            {
                operateContext.bllSession.T_VRAddress.Modify(new T_VRAddress { is_default = false }, s => s.user_id == user_id && s.id != dto.id, "is_default");
                operateContext.bllSession.T_VRAddress.Modify(new T_VRAddress { is_default = true }, s => s.id == dto.id, "is_default");
            }
            else
            {
                //取消设置
                if(!obj.is_default)
                    return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "当前收获地址非默认无需取消默认设置~", "");
                else
                    operateContext.bllSession.T_VRAddress.Modify(new T_VRAddress { is_default = false }, s => s.id == dto.id, "is_default");
            }
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "更改默认地址设置成功~", "");
        }
        #endregion
    }
}